import { faker } from "@faker-js/faker";
import { defineFakeRoute } from "vite-plugin-fake-server/client";

export default defineFakeRoute([
  {
    url: "/mock/user/login",
    method: "post",
    response: ({ body }) => {
      return {
        error: "",
        status: 1,
        data: {
          account: body.account,
          token: `${body.account}:${faker.internet.jwt()}`,
          avatar: "https://fantastic-admin.hurui.me/logo.svg",
        },
      };
    },
  },
  {
    url: "/mock/user/permission",
    method: "get",
    response: ({ headers }) => {
      let permissions: string[] = [];
      if (headers.token?.indexOf("admin") === 0) {
        permissions = [
          "permission.browse",
          "permission.create",
          "permission.edit",
          "permission.remove",
        ];
      } else if (headers.token?.indexOf("test") === 0) {
        permissions = ["permission.browse"];
      }
      return {
        error: "",
        status: 1,
        data: {
          permissions,
        },
      };
    },
  },
  {
    url: "/mock/user/password/edit",
    method: "post",
    response: () => {
      return {
        error: "",
        status: 1,
        data: {
          isSuccess: true,
        },
      };
    },
  },
]);
